package com.itheima.project_cloth.controller;

import com.itheima.project_cloth.mapper.ClothAdd;
import com.itheima.project_cloth.pojo.OrderMerchant;
import com.itheima.project_cloth.pojo.PageBean;
import com.itheima.project_cloth.pojo.Result;
import com.itheima.project_cloth.service.OrderMerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/orderMerchants")
public class OrderMerchantController {
    @Autowired
    private OrderMerchantService orderMerchantService;

    @Autowired
    private ClothAdd clothAdd;

    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public Result InsertOrderMerchan(@RequestBody OrderMerchant orderMerchant) {
        log.info("新增订单信息为:", orderMerchant);
        orderMerchantService.insertOrderMerchan(orderMerchant);
        clothAdd.update(orderMerchant);
        return Result.success();

    }

    @GetMapping("/select")
    public Result SelectOrderMerchantByTime(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer maxPage, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        log.info("查询的下单时间为:", beginTime);
        PageBean orderMerchantList = orderMerchantService.selectByTime(page,maxPage,beginTime, endTime);
        return Result.success(orderMerchantList);
    }

    @GetMapping
    public Result selectOrderMerchantByName(String name){
        OrderMerchant orderMerchant = orderMerchantService.selectOrderMerchantByName(name);

        return Result.success(orderMerchant);
    }

    @GetMapping("/{name}")
    public Result selectOrderMerchantByCloth(@PathVariable String name){
        log.info("获得的参数为{}", name);
        List<OrderMerchant> list = orderMerchantService.selectOrderMerchantByCloth(name);

        return Result.success(list);
    }
}
